#!/usr/bin/env python
# -*- coding: latin-1; py-indent-offset:4 -*-
################################################################################
# 
# This file is part of Bfplusplus
#
# Bfplusplus is a graphical interface to the Betfair Betting Exchange
# Copyright (C) 2010 Daniel Rodriguez (aka Daniel Rodriksson)
# Copyright (C) 2011 Sensible Odds Ltd.
#
# You can learn more and contact the author at:
#
#    http://code.google.com/p/bfplusplus/
#
# Bfplusplus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bfplusplus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bfplusplus. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""Subclass of EditPattern, which is generated by wxFormBuilder."""

import wx

import MainGui

from EditString import EditString
from EditStringList import EditStringList


# Implementing EditPattern
class EditPattern(MainGui.EditPattern):

    def __init__(self, parent, name, pattern, patternNames=None):
	MainGui.EditPattern.__init__(self, parent)

	self.name = name
	self.pattern = pattern
        self.patternNames = patternNames

	self.m_textCtrlPatternName.SetValue(self.name)

        marketNames = ' or '.join(self.pattern[0])
        self.m_textCtrlPatternMarketNames.SetValue(marketNames)

        for posMenuPatterns in self.pattern[1]:
            posMenuPattern = ' or '.join(posMenuPatterns)
            self.m_listBoxPatternMenuNamesPositive.Append(posMenuPattern, posMenuPatterns)

        negMenuPattern = ' or '.join(self.pattern[2])
        self.m_textCtrlPatternMenuNegative.SetValue(negMenuPattern)

	
    # Handlers for EditPattern events.
    def OnButtonClickPatternNameEdit(self, event):

	dlg = EditString(self, self.name)
        dlg.SetTitle('Edit Pattern Name')
	retcode = dlg.ShowModal()
	if retcode != wx.ID_OK:
	    return

        if not dlg.value:
            wx.MesageBox('The pattern name cannot be empty', 'No pattern name')
            return

	self.m_textCtrlPatternName.SetValue(dlg.value)


    def OnButtonClickPatternMarketNamesEdit(self, event):
        valuesStr = self.m_textCtrlPatternMarketNames.GetValue()
        if valuesStr:
            marketNames = valuesStr.split(' or ')
        else:
            marketNames = list()
        dlg = EditStringList(self, marketNames)

	retcode = dlg.ShowModal()
	if retcode != wx.ID_OK:
	    return

        valuesStr = ' or '.join(dlg.values)
        self.m_textCtrlPatternMarketNames.SetValue(valuesStr)


    def OnButtonClickPatternMenuNamesPositiveDelete(self, event):
        itemIndex = self.m_listBoxPatternMenuNamesPositive.GetSelection()
        if itemIndex == wx.NOT_FOUND:
            return

        values = self.m_listBoxPatternMenuNamesPositive.Delete(itemIndex)
        

    def NewOrEditPatternMenuNamesPositive(self, itemIndex=-1):
        if itemIndex != -1:
            values = self.m_listBoxPatternMenuNamesPositive.GetClientData(itemIndex)
        else:
            values = []

        dlg = EditStringList(self, values)
        retcode = dlg.ShowModal()
        if retcode != wx.ID_OK:
            return
        if not dlg.values:
            return

        if itemIndex != -1:
            self.m_listBoxPatternMenuNamesPositive.Delete(itemIndex)

        valuesStr = ' or '.join(dlg.values)
        self.m_listBoxPatternMenuNamesPositive.Append(valuesStr, dlg.values)

	
    def OnButtonClickPatternMenuNamesPositiveNew(self, event):
        self.NewOrEditPatternMenuNamesPositive()


    def OnButtonClickPatternMenuNamesPositiveEdit(self, event):
        itemIndex = self.m_listBoxPatternMenuNamesPositive.GetSelection()
        if itemIndex == wx.NOT_FOUND:
            return

        self.NewOrEditPatternMenuNamesPositive(itemIndex)

	
    def OnButtonClickPatternMenuNamesNegativeEdit(self, event):
        valuesStr = self.m_textCtrlPatternMenuNegative.GetValue()
        if valuesStr:
            negMatches = valuesStr.split(' or ')
        else:
            negMatches = list()
        dlg = EditStringList(self, negMatches)

	retcode = dlg.ShowModal()
	if retcode != wx.ID_OK:
	    return

        valuesStr = ' or '.join(dlg.values)
        self.m_textCtrlPatternMenuNegative.SetValue(valuesStr)


    def MoveUpDown(self, direction):
	if direction > 0:
	    limit = self.m_listBoxPatternMenuNamesPositive.GetCount() - 1
	else:
	    limit = 0
	selection = self.m_listBoxPatternMenuNamesPositive.GetSelection()

	if selection == wx.NOT_FOUND or selection == limit:
	    return

	stringItem = self.m_listBoxPatternMenuNamesPositive.GetString(selection)
        values = self.m_listBoxPatternMenuNamesPositive.GetClientData(selection)
	self.m_listBoxPatternMenuNamesPositive.Delete(selection)
	selection += direction
	self.m_listBoxPatternMenuNamesPositive.Insert(stringItem, selection)
        self.m_listBoxPatternMenuNamesPositive.Select(selection)
        self.m_listBoxPatternMenuNamesPositive.SetClientData(selection, values)


    def OnButtonClickMoveUp(self, event):
        self.MoveUpDown(-1)


    def OnButtonClickDown(self, event):
        self.MoveUpDown(1)


    def OnOKButtonClick(self, event):

        name = self.m_textCtrlPatternName.GetValue()

	if not name:
	    wx.MessageBox('A pattern must have a name to be saved',
			  'No name')
	    return

        if self.patternNames and name in self.patternNames:
	    retcode = wx.MessageBox('The name of the pattern already exists.\n'
                                    '\n'
                                    'Press Yes to overwrite or No to continue editing',
                                    'Duplicate name', wx.YES_NO)

            if retcode != wx.YES:
                return

        # Pattern name
	self.name = name

        # Market names
        valuesStr = self.m_textCtrlPatternMarketNames.GetValue()
        if valuesStr:
            self.pattern[0] = valuesStr.split(' or ')
        else:
            self.pattern[0] = list()

        # Positive matches
        count = self.m_listBoxPatternMenuNamesPositive.GetCount()
        self.pattern[1] = list()
        for itemIndex in xrange(count):
            values = self.m_listBoxPatternMenuNamesPositive.GetClientData(itemIndex)
            self.pattern[1].append(values)

        # Negative matches
        valuesStr = self.m_textCtrlPatternMenuNegative.GetValue()
        if valuesStr:
            self.pattern[2] = valuesStr.split(' or ')
        else:
            self.pattern[2] = list()

        self.EndModal(wx.ID_OK)
